/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.Map;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;
public class SynonymTokenFilter extends ElasticFilter{

	
	public static final String SYNONYMS_PATH_PARAM_KEY = "synonyms_path";
	public static final String IGNORE_CASE_PARAM_KEY = "ignore_case";
	public static final String EXPAND_PARAM_KEY = "expand";
	
	
	
	private String synonymsPath;
	private Boolean ignoreCase;
	private Boolean expand;
	
	
	
	public SynonymTokenFilter(String defName){
		this(null,defName);
	}
	
	
	public SynonymTokenFilter(Map<String,String> params, String defName){
		
		super(ElasticFilterType.synonym, defName);
		
		if(params!=null){
			this.synonymsPath = params.get(SYNONYMS_PATH_PARAM_KEY); 
			
			String ignoreCaseParam = params.get(IGNORE_CASE_PARAM_KEY);
			if(ignoreCaseParam!=null && !ignoreCaseParam.isEmpty()){
				this.ignoreCase = Boolean.parseBoolean(ignoreCaseParam);
			}
			String expandParam = params.get(EXPAND_PARAM_KEY);
			if(expandParam!=null && !expandParam.isEmpty()){
				this.expand = Boolean.parseBoolean(expandParam);
			}
			
		}
	}
	
	
	@Override
	public boolean hasCustomParams() {
		 if( 
			(this.synonymsPath!=null && !this.synonymsPath.isEmpty()) ||
			this.ignoreCase != null ||
			this.expand != null
		 )return true;
		 return false;
	}
	
	
	

	@Override
	public XContentBuilder addToBuilder(XContentBuilder mappingBuilder) throws IOException {
		mappingBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		
		 
		if(this.synonymsPath != null && !this.synonymsPath.isEmpty()){
			mappingBuilder.field(SYNONYMS_PATH_PARAM_KEY, this.synonymsPath);
		}
		if(this.ignoreCase != null){
			mappingBuilder.field(IGNORE_CASE_PARAM_KEY, this.ignoreCase.booleanValue());			
		}
		if(this.expand != null){
			mappingBuilder.field(EXPAND_PARAM_KEY, this.expand.booleanValue());
		}
		return mappingBuilder;
	}
	
	
	@Override
	public void setParamsFromSettings(Settings settings) {
		 
		String synonymsPathSetting = settings.get(SYNONYMS_PATH_PARAM_KEY);
		if(synonymsPathSetting!=null && !synonymsPathSetting.isEmpty()){
			this.synonymsPath = synonymsPathSetting;
		}
		
		String ignoreCaseSetting = settings.get(IGNORE_CASE_PARAM_KEY);
		if(ignoreCaseSetting!=null && !ignoreCaseSetting.isEmpty()){
			this.ignoreCase = Boolean.parseBoolean(ignoreCaseSetting);
		}
		
		String expandSetting = settings.get(EXPAND_PARAM_KEY);
		if(expandSetting!=null && !expandSetting.isEmpty()){
			this.expand = Boolean.parseBoolean(expandSetting);
		}
	}
	


	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition other){
		
		if(other==null)return false;
		SynonymTokenFilter o;
		try {
			o = (SynonymTokenFilter)other;
		} catch (ClassCastException e) {
			return false;
		}
		
		return 
		EqualsUtil.areEqual(this.synonymsPath, o.synonymsPath)
		&&
		EqualsUtil.areEqual(this.ignoreCase, o.ignoreCase)
		&&
		EqualsUtil.areEqual(this.expand, o.expand);
	}
	
	
	
}
